// pages/webViewPage/webViewPage.js

import { loginWxAction } from "../../service/config"
import { bindUserRelation } from "../../service/user_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: "",
    avatar: "",//用户头像,
    nickName: "",//用户昵称
    name: "",//用户真实姓名
    phone: "",//用户手机号
    openId: null,//用户openId
    userId: "",
    isShare: false,//是否是分享进来的
    bindUserId: "",
    isShowShareBtn:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type == 3) {
      this.setData({
        isShowShareBtn: true,
      })
    }

    if (options.webUrl) {
      this.setData({
        url: options.webUrl,
        isShare: true,
        name: options.name,
        avatar: options.avatar,
        phone: options.phone,
        bindUserId: options.userId,
        openId: options.openId
      })
      this.userWxLogin();
      return;
    }

    this.setData({
      url: app.globalData.webUrl,
      avatar: app.globalData.avatar,
      nickName: app.globalData.nickName,
      name: app.globalData.name,
      phone: app.globalData.phone,
      openId: app.globalData.openId,
      userId: app.globalData.userId,
    })
    console.log("JJLog:url:" + JSON.stringify(this.data.url));
    wx.showLoading({
      title: "加载中...",
    })
  },
  //拨打电话
  callPhoneAction() {
    if (!this.data.phone) {
      wx.showToast({
        title: '暂时无法电话咨询',
        icon: "none"
      })
    }
    wx.makePhoneCall({
      phoneNumber: this.data.phone,
      fail: err => {

      }
    })
  },
  chatAction() {
    let chatData = {
      name: this.data.name,
      avatar: this.data.avatar,
      chatUserId: this.data.openId,
      conversationType: "C2C",
    }
    app.globalData.chatData = chatData;
    wx.navigateTo({
      url: '/firstPackage/pages/msgModule/chatRoom/chatRoom',
    })
  },

  //用户登陆授权等:
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickName;
        app.globalData.avatar = data.avatar;
        app.globalData.name = data.name || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.id;
        app.globalData.qrCode = data.qrCode || "";
        app.globalData.isAuthUserInfo = data.isAuthUserInfo;
        app.globalData.weChat = data.weChat || "";
        app.globalData.roleName = data.roleName || "";
        app.globalData.companyName = data.companyName || "";
        app.globalData.companyNumber = data.companyNumber || "";
        app.globalData.openId = data.openId;
        //绑定挂级关系:
        if (this.data.bindUserId) {
          this.bindUserRelation();
        }
        this.loginTIMAction(data.openId, res.data.userSig);



      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);
    })
  },
  //绑定挂级关系:
  bindUserRelation() {

    bindUserRelation(2, this.data.bindUserId, "").then(res => {
      console.log("JJLog:绑定挂级关系:" + JSON.stringify(res));
      if (res.code == 200) {

      }
    }).catch(err => {
      console.error("JJLog:bindUserRelation:", err);
    })
  },
  //登录TIM===============:
  loginTIMAction(userId, userSig) {
    let promise = app.globalData.tim.login({ userID: userId, userSig: userSig });
    promise.then(function (imResponse) {
      console.log("JJLog:登录tim成功:" + JSON.stringify(imResponse.data)); // 登录成功
      if (imResponse.data.repeatLogin === true) {
        // 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
        console.log(imResponse.data.errorInfo);
      }
    }).catch(function (imError) {
      console.error('JJLog登录腾讯TIM失败===:', imError); // 登录失败的相关信息
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  loadSuccess() {
    wx.hideLoading();
  },
  loadError() {
    wx.hideLoading();
    wx.showToast({
      title: '获取失败',
      icon: "none"
    })
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let name = this.data.name || this.data.nickName;
    let avatar = this.data.avatar
    let phone = this.data.phone || "";
    let userId = this.data.userId || ""
    let openId = this.data.openId || ""
    let path = "/pages/webViewPage/webViewPage?isShare=1" + "&webUrl=" + app.globalData.webUrl + "&name=" + name + "&avatar=" + avatar + "&phone=" + phone + "&userId=" + userId + "&openId=" + openId
    return {
      title: app.globalData.commonShareTitle,
      path: path
    }
  },



})